import {defineStore} from "pinia";
/*
import {cloneDeep} from "../../../utils/cloneDeep";
import {loginnew, reqLogin, reqUserInfo} from "../../../api/user/user";
import type {
    loginFormData,
    loginResponseData,
    userInfoReponseData,
} from '../../../api/user/type'

 */
import {cookie} from "../../../utils/cookie";

import type {UserType} from './userType'

import router from "../../../router";
import {asnycRoute,constantRoute,anyRoute} from "@/router/routes";
import {useRoute} from "vue-router";
import {loginParams, loginResult} from "@/api/login/type";
import {loginReq, rxOldLoginReq} from "@/api/login";
//用于过滤当前用户需要展示的异步路由
function filterAsyncRoute(asnycRoute: any, routes: any) {
    return asnycRoute.filter((item: any) => {
        if (routes.includes(item.name)) {
            if (item.children && item.children.length > 0) {
                item.children = filterAsyncRoute(item.children, routes)
            }
            return true
        }
    })
}


let useUserStore = defineStore('User', {
    state: (): UserType => {
        return {
            username: '',
            avatar: '',
            buttons: [],
            token: cookie.getCookie('token'),
            menuRoutes: constantRoute,
            allRoutes:[]
        }
    },
    actions: {
        async login(params: loginParams) {
            let res = await loginReq(params)
            if (res.code === 0) {
                let token = res.data.token as string
                this.username = res.data.username
                console.log(this.username,'---------------')
                this.token = {
                    name: 'token',
                    value: token
                }
                cookie.setCookie('token', token, new Date().getTime())
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },
        async userInfo() {
            //获取用户信息进行存储仓库当中[用户头像、名字]
            // const result: userInfoReponseData = await reqUserInfo()
            // //如果获取用户信息成功，存储一下用户信息
            // console.log(result)
            // if (result.code == 200) {
            //     this.username = result.data.name
            //     this.avatar = result.data.avatar
            //     this.buttons = result.data.buttons
            //     //计算当前用户需要展示的异步路由
            //     const userAsyncRoute = filterAsyncRoute(
            //         cloneDeep(asnycRoute),
            //         result.data.routes,
            //     )
            //     //菜单需要的数据整理完毕
            //     this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute]
            //     //目前路由器管理的只有常量路由:用户计算完毕异步路由、任意路由动态追加
            //     ;[...userAsyncRoute, anyRoute].forEach((route: any) => {
            //         router.addRoute(route)
            //     })
            //     this.allRoutes=router.getRoutes()
            //     console.log(this.allRoutes)
            //     return 'ok'
            // } else {
            //     return Promise.reject(new Error(result.message))
            // }
            let getInfo=()=>{
                return new Promise((resolve,reject)=>{
                    this.username = ''
                    this.avatar = ''
                    this.buttons = []
                    //菜单需要的数据整理完毕
                    this.menuRoutes = [...constantRoute, ...asnycRoute, anyRoute]
                    //目前路由器管理的只有常量路由:用户计算完毕异步路由、任意路由动态追加
                    ;[...asnycRoute, anyRoute].forEach((route: any) => {
                        router.addRoute(route)
                    })
                    this.allRoutes=router.getRoutes()
                    // console.log(this.allRoutes)
                    resolve(this.allRoutes)
                })
            }
            await getInfo()
        },
        async rxOldLogin(params: {token:string}) {
            let res = await rxOldLoginReq(params)
            if (res.code === 0) {
                let token = res.data.token as string
                this.username = res.data.username
                console.log(this.username)
                this.token = {
                    name: 'token',
                    value: token
                }
                window.localStorage.setItem('userLoginMsg', JSON.stringify({
                    pwd: '',
                    username: res.data.username
                }))
                cookie.setCookie('token', token, new Date().getTime())
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },
    },
    getters: {}
})

export default useUserStore;
